<template>
  <div class="app-container calendar-list-container">
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
            <el-form-item label="原密码" prop="oldPassword">
              <el-input type="password" v-model="ruleForm2.oldPassword" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="newPassword">
              <el-input type="password" v-model="ruleForm2.newPassword" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPassword">
              <el-input type="password" v-model="ruleForm2.checkPassword" auto-complete="off" ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
              <el-button @click="resetForm('ruleForm2')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { getToken } from "@/util/auth";
import request from "@/router/axios";
import url from '@/const/apiURL'  
import md5 from 'js-md5';
import { Message } from 'element-ui'

export default {
  data() {
    var validateCheckPass = (rule, value, callback) => {
      if (this.ruleForm2.newPassword !== '') {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.ruleForm2.checkPassword) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      ruleForm2: {
        oldPassword: '',
        newPassword: '',
        checkPassword: ''
      },
      rules2: {
        oldPassword: [{  required: true, trigger: 'blur',message:"旧密码必填" }],
        newPassword: [{  required: true, trigger: 'blur',message:"新密码必填" }],
        checkPassword: [{  required: true, trigger: 'blur',message:"确认密码必填",validator:validateCheckPass }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const params = {
              oldPassword:md5(this.ruleForm2.oldPassword),
              newPassword:md5(this.ruleForm2.newPassword),
              checkPassword:md5(this.ruleForm2.checkPassword)
          }
          request({
            url: url.USER_UPDATE_PASSWORD_URL,
            method: 'POST',
            data: params
          }).then(response => {
              if (response) {
                Message({
                  title: '成功',
                  message: '修改成功',
                  type: 'success'
                });
                // 修改密码之后强制重新登录
                if (this.ruleForm2.checkPassword !== '') {
                  this.$store.dispatch('LogOut').then(() => {
                    location.reload() 
                  });
                } else {
                  this.$router.push({ path: '/' })
                }
              } else {
                Message({
                  title: '失败',
                  message: response.data.msg,
                  type: 'error'
                })
              }
            })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
};
</script>